<template>
  <div class>
    <nly-container :fluid="true">
      <nly-breadcrumb>
        <nly-breadcrumb-item
          icon="nlyfont nly-icon-home-fill"
          to="/"
          text="首页"
        />
        <nly-breadcrumb-item
          icon="nlyfont nly-icon-outlet"
          to="/food"
          active
          text="优惠券券码管理"
        />
      </nly-breadcrumb>

      <section class="content">
        <br />
        <Table
          border
          :columns="header"
          :data="accountList"
          @on-selection-change="enterall"
          ref="selection"
        >
         <template slot-scope="{ row }" slot="haveUse">{{
              row.haveUse == "0" ? "未使用" :  "已使用"
            }}</template>

          <template slot-scope="{ row }" slot="useAccount">{{
              row.useAccount === "" ? "无" :  row.useAccount
            }}</template>
          <template slot-scope="{ row }" slot="action">
            <Button
              style="margin-right: 10px"
              type="error"
              size="small"
              @click="remove(row.id)"
              >删除</Button
            >
          </template>
        </Table>
        <br />
        <div class="page">
          <Page
            :total="total"
            show-elevator
            show-total
            :page-size="data.pageSize"
            :current="data.page"
            @on-change="changePage"
          />
        </div>
      </section>
    </nly-container>
  </div>
</template>
<script>
import apis from "@/api/shop1";
export default {
  components: {},
  data() {
    return {
      modalAddText: false,
      modalAddDiscountCode: false,
      update: false,
      searchText: "",
      total: 0, 

      data: {
        page: 1, 
        pageSize: 10, 
        type: 1,
      },
      header: [
        {
          title: "优惠券码",
          key: "code",
          align: "center",
        },
        {
          title: "使用",
          slot: "haveUse",
          align: "center",
        },
        {
          title: "创建时间",
          key: "createTime",
          align: "center",
        },

        {
          title: "操作",
          slot: "action",
          width: 150,
          fixed: "right",
          align: "center",
        },
      ],
      accountList: [],
      count_scale: [],
      pageNow: 1,
      selectclass: [],
      dataText: {
        name: "",
        meet: "",
        subtract: "",
        outTime: "",
        price: "",
        discount: "",
        maximum: "",
        state: "",
      },
      codeText: {
        discountId: "",
        count: "",
      },

      key: "",
      id: "",
    };
  },
  mounted() {
    let that = this;
    if (typeof that.$route.query.id != "undefined") {
      that.data.id = that.$route.query.id
    }
    that.getcupponlist();
  },
  methods: {

    addCodeModal(id) {
      this.codeText.discountId = id
      this.modalAddDiscountCode = true
    },
    addCodeText() {
      apis.saveDiscountCode(this.codeText)
      .then((response) => {
        if (response.data.code === 0) {
          this.$Message.success("添加优惠券码成功!")
          this.modalAddDiscountCode = false
        }else {
          this.$Message.error("添加失败!")
        }
      }).catch((error) => {
        console.log(error)
        this.modalAddDiscountCode = false
        this.$Message.error("添加失败")
      })
    },
    changeType(val) {
      this.data.page = 1;
      this.data.type = val;
      console.log(this.data2);
      this.getcupponlist();
    },
    addaccount(id) {
      this.$router.push({
        path: "/addaccoundq",
        query: {
          id: id,
        },
      });
    },
    addShow() {
      this.modalAddText = true;
    },
    enterall(e) {
      this.enteralldata = e;
      console.log(e);
    },
    remove(id) {
      apis
        .removeDiscountCode({ id: id })
        .then((response) => {
          console.log(response);
          if (response.data.code === 0) {
            this.$Message.success("删除成功");
            this.getcupponlist();
          } else {
            this.$Message.error("删除失败!");
          }
        })
        .catch((error) => {
          console.log(error);
          this.loading = false;
          this.$Message.error("删除失败!");
        });
    },
    getcupponlist() {
      let that = this;
      apis.getDiscountCodeList(this.data).then((response) => {
        let resdata = response.data;
        if (resdata.code === 0) {
          console.log(response.data.data.records);
          that.accountList = response.data.data.records;
           that.total = resdata.data.total;
        }
      });
    },
    changePage(page) {
      this.data.page = page;
      this.getcupponlist();
    },
    search_account(val) {
      this.data.page = 1;
      let data = this.data;
      data.name = val;
      this.getcupponlist();
    },
    ModalClose() {
      this.modalAddText = false;
      this.update = false;
      this.modalAddDiscountCode = false;
    },
    changeText(id, data) {
      console.log("打开模态框");
      this.update = true;
      this.dataText.name = data.name;
      this.dataText.meet = data.meet;
      this.dataText.subtract = data.subtract;
      this.dataText.outTime = data.outTime;
      this.dataText.discount = data.discount;
      this.dataText.price = data.price;
      this.dataText.type = data.type;
      this.dataText.state = data.state;
      this.dataText.maximum = data.maximum;
      this.modalAddText = true;
      this.id = id;
      console.log(this.id);
    },
    AddText(name) {
      console.log(name);
      console.log(this.update);
      var that = this;
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.update) {
            this.dataText.id = this.id;
            apis
              .updatecuppon({
                id: this.dataText.id,
                name: this.dataText.name,
                outTime: this.dataText.outTime,
                meet: this.dataText.meet,
                discount: this.dataText.discount,
                price: this.dataText.price,
                type: this.dataText.type,
                state: this.dataText.state,
                maximum: this.dataText.maximum,
              })
              .then((response) => {
                if (response.data.code == 0) {
                  this.$Message.success("修改成功");
                  that.getcupponlist();
                  this.modalAddText = false;
                  this.update = false;
                  this.dataText.subtract = "";
                  this.$refs.dataText.resetFields();
                } else {
                  this.$Message.error("修改失败");
                }
              });
          } else {
            apis
              .addcuppon({
                name: this.dataText.name,
                outTime: this.dataText.outTime,
                meet: this.dataText.meet,
                subtract: this.dataText.subtract,
                discount: this.dataText.discount,
                price: this.dataText.price,
                type: this.dataText.type,
                state: this.dataText.state,
                maximum: this.dataText.maximum,
              })
              .then((response) => {
                this.loading = false;
                let resdata = response.data;
                if (resdata.code == 0) {
                  this.$refs.dataText.resetFields();
                  this.modalAddText = false;
                  this.update = false;
                  this.dataText.subtract = "";
                  this.$Message.success("添加成功");
                  that.getcupponlist();
                } else if (resdata.code == 201) {
                  this.$Message.success("账号重复添加");
                }
              });
          }
        }
      });
    },
    cancel() {
      this.$refs.dataText.resetFields();
      this.dataText.name = "";
      this.dataText.maximum = "";
      this.dataText.meet = "";
      this.dataText.subtract = "";
      this.dataText.discount = "";
      this.dataText.outTime = "";
      this.dataText.price = "";
      this.dataText.state = "";
      this.dataText.type = "";
      this.modalAddText = false;
      this.update = false;
    },
  },
};
</script>

<style scoped>
.padding20 {
  margin: 20px;
}

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 90px;
  margin: 5px;
}
.Page {
  display: flex;
  justify-content: center;
  text-align: center;
  padding-bottom: 10px;
}
.ivu-modal-body {
  padding: 16px;
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
}
.page {
  text-align: center;
}
</style>